import { command, model, schema } from '@/ts/base';
import { IFile, IPerson, ITarget, XCollection, XObject } from '..';
import { Xbase } from '@/ts/base/schema';
const WEBSITE_COLLNAME = '-public-website'; //-public-website
/** 网站方法提供者 */
export class WebSiteProvider {
  private cacheObj: XObject<Xbase>;
  private target: ITarget;
  private publicTemplateColl: XCollection<schema.XWebsite & schema.Xbase>;
  loaded: boolean = false;
  private tabs: schema.XWebsite[] = [];
  portalConfig: model.PortalConfig = { commons: [] };
  constructor(_target: ITarget, _user: IPerson) {
    this.cacheObj = _target.cacheObj;
    this.target = _target;
    this.publicTemplateColl = _target.resource.genColl(WEBSITE_COLLNAME);
  }
  get bannerKey() {
    return 'website';
  }

  async loadConfig(reload: boolean = false) {
    if (!reload && this.portalConfig.commons.length > 0) {
      return this.portalConfig.commons;
    }
    this.portalConfig = (await this.cacheObj.get('portalConfig')) || {
      commons: [
        { text: '首页', bodyType: 'home', status: 1 },
        { text: '工作台', bodyType: 'bench', status: 1 },
        { text: '消息', bodyType: 'chat', status: 1 },
        { text: '待办', bodyType: 'work', status: 1 },
        { text: '动态', bodyType: 'activity', status: 1 },
        { text: '文件', bodyType: 'store', status: 1 },
        { text: '成员', bodyType: 'relation', status: 1 },
        { text: '智能体', bodyType: 'smart', status: 1 },
      ],
    };

    return this.portalConfig.commons;
  }

  async changeCommonTabStatus(item: any, checked: boolean) {
    if (!this.portalConfig.commons.some((i) => i.bodyType === item.bodyType)) {
      this.portalConfig.commons.push(item);
    }
    const commons: any[] = this.portalConfig.commons.map((i) => {
      if (i.bodyType === item.bodyType) {
        return { ...i, status: checked ? 1 : 0 };
      }
      return i;
    });
    this.portalConfig.commons = commons;
    await this.cacheObj.set('portalConfig', this.portalConfig);
    command.emitterFlag('topChanged', 'commonChanged');
  }

  //是否公开
  async loadPageStatus() {
    // const result: boolean | undefined = await this.cacheObj.get<boolean>('showWebsite');
  }

  async loadTabs(reload: boolean = false) {
    if (this.tabs.length > 0 && !reload) {
      return this.tabs;
    }
    this.loaded = false;
    const options = { match: { isDeleted: false } };
    this.tabs = await this.publicTemplateColl.loadSpace({ options });
    this.loaded = true;
    return this.tabs;
  }

  async updateTabs(data: schema.XWebsite) {
    if (!data.id) {
      data.id = 'snowId()';
    }
    const res = await this.publicTemplateColl.replace(data as any);
    if (res && res.id) {
      await this.loadTabs(true);
    }
  }
  async removeTabs(data: schema.XWebsite) {
    const res = await this.publicTemplateColl.remove(data as any);
    if (res) {
      this.tabs = this.tabs.filter((item) => item.id !== data.id);
    }
  }

  async searchFiles(commons: schema.XWebsite[]) {
    let files: any[] = [];

    for (const common of commons) {
      const target = this.target.targets.find(
        (i) => i.id === common.targetId && i.spaceId === common.spaceId,
      );
      if (!target) continue;
      const file: IFile | undefined = await target.directory.searchComment({
        ...common,
        id: common.entityId,
      });
      if (file && !file.groupTags.includes('已删除')) {
        files.push({ ...common, file });
      }
    }
    return files;
  }
}
